package org.zhq.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.zhq.ServerResult;
import org.zhq.pojo.Items;
import org.zhq.service.ItemsEsService;


@Api(value = "商品", tags = {"商品展示相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController {

    private final ItemsEsService itemsService;

    @Autowired
    public ItemsController(ItemsEsService itemsService) {
        this.itemsService = itemsService;
    }


    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
    @GetMapping("/search")
    public ServerResult search(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序方式")
            @RequestParam String sort,
            @ApiParam(name = "page", value = "页码")
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize", value = "页距")
            @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        if (StrUtil.isBlank(keywords)) {
            return ServerResult.errorMsg("商品不存在");
        }
        page--;
        IPage<Items> pageResult = itemsService.searchItems(keywords, sort, page, pageSize);
        return ServerResult.ok(pageResult);
    }


}
